package com.itheima.reggie.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Service.CategoryService;
import com.itheima.reggie.exception.R;
import com.itheima.reggie.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController()
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    //添加
    public R<String> save(@RequestBody Category category)
    {
        categoryService.save(category);
        return R.success("添加成功");
    }
    //分页
    @GetMapping("/page")
    public  R<Page> page(int page,int pageSize)
    {
        Page pageInfo=new Page(page,pageSize);
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
         categoryService.page(pageInfo,queryWrapper);

        return  R.success(pageInfo);
    }
    //删除
    @DeleteMapping
    public  R<String> delect(Long ids)
    {
        categoryService.removeId(ids);
        return R.success("删除成功");
    }
    //修改
    @PutMapping
    public  R<String > update(@RequestBody Category category)
    {
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    //查询菜品分类
    @GetMapping("/list")
    public R<List<Category>> list(Category category)
    {
  QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
  if(category.getType()!=null) {
      queryWrapper.eq("type", category.getType());
  }
  queryWrapper.orderByDesc("sort");
  List<Category> list=categoryService.list(queryWrapper);
  return   R.success(list);
    }
}
